package licode.unisop.realm.application.util;

import cn.hutool.core.util.StrUtil;
import licode.unisop.realm.domainx.dp.Account;
import licode.unisop.realm.domainx.dp.AccountCategory;
import licode.unisop.realm.domainx.model.AccountVo;
import licode.unisop.realm.domainx.model.AuthInfoVo;

import java.util.ArrayList;
import java.util.List;

public class AccountUtil {
    public static List<AccountVo> addAccount(List<AccountVo> accounts,
                           String category, String account) {
        List<AccountVo> retAcc = accounts;
        if (null == retAcc) {
            retAcc = new ArrayList<>();
        }
        if (StrUtil.isNotBlank(account)) {
            AccountCategory cg = AccountCategory.build(category);
            if (null != accounts) {
                accounts.removeIf((AccountVo item) ->
                        item.getCategory().equals(cg));
                accounts.add(new AccountVo("", new Account(account), cg));
            }
        }
        return retAcc;
    }

    public static void removeAccount(List<AccountVo> accounts, String category) {
        AccountCategory cg = AccountCategory.build(category);
        accounts.removeIf((AccountVo item) ->
                item.getCategory().equals(cg));
    }

    public static boolean validAccounts(List<AccountVo> accounts) {
        return accounts.stream()
                .map((AccountVo item) -> item.getCategory().getCategory())
                .distinct().count() == accounts.size();
    }

    public static List<AuthInfoVo> addAuthInfo(List<AuthInfoVo> authInfos,
                                              AuthInfoVo authInfoVo) {
        List<AuthInfoVo> retAuth = authInfos;
        if (null == retAuth) {
            retAuth = new ArrayList<>();
        }
        if (null != authInfoVo && StrUtil.isNotBlank(authInfoVo.getAuthType())) {
            if (null != authInfos) {
                authInfos.removeIf((AuthInfoVo item) ->
                        item.getAuthType().equals(authInfoVo.getAuthType()));
                authInfos.add(authInfoVo);
            }
        }
        return retAuth;
    }

    public static boolean validAuthInfos(List<AuthInfoVo> authInfos) {
        return authInfos.stream()
                .map(AuthInfoVo::getAuthType)
                .distinct().count() == authInfos.size();
    }
}
